<template>
  <div class="bg-box">
    <t-form label-align="top">
      <t-row :gutter="20">
        <t-col :span="6">
          <t-form-item :label="plugin.t('order.ticket')">
            <t-select v-model="form.ticket" @change="onCode">
              <t-option value="print" :label="plugin.t('order.ticket_text.print')" />
              <t-option value="print_refund" :label="plugin.t('order.ticket_text.print_refund')" />
              <t-option value="ticket" :label="plugin.t('order.ticket_text.ticket')" />
              <t-option value="ticket_refund" :label="plugin.t('order.ticket_text.ticket_refund')" />
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="6">
          <t-form-item :label="plugin.t(type + '.code')">
            <t-input :value="codeShow" readonly />
          </t-form-item>
        </t-col>
      </t-row>
      <t-row :gutter="20" style="margin-top: 15px">
        <t-col :span="6">
          <t-form-item :label="plugin.t('order.date')">
            <t-date-picker v-model="form.date" :format="app.time.format()" style="width: 100%" />
          </t-form-item>
        </t-col>
        <t-col :span="6">
          <t-form-item :label="plugin.t('order.status_time')">
            <t-date-picker 
              v-model="form.status_time" 
              :format="app.time.format()" 
              valueType="Date" 
              :disableDate="{ before: app.time.now().quarterStart().toDate(), after: app.time.now().quarterEnd().toDate() }"
              style="width: 100%" 
            />
          </t-form-item>
        </t-col>
      </t-row>
    </t-form>
  </div>
</template>
<script setup>
  import { ref, onUnmounted } from 'vue'
  import app from '@/app'
  import index from '../../index'
  import store from './store'
  import { code } from '@/api/erp/config'

  const plugin = index.plugin
  const form = store.data
  const type = store.type
  const codeShow = ref('')

  const onCode = () => {
    code(type.value == 'expect' ? 'expect' : form.value.ticket, true).then(res => {
      codeShow.value = res.code
    })
  }

  
  // 是否存在Code
  if(form.value.code) {
    codeShow.value = form.value.code
  } else {
    // 预测Code
    onCode()
    const timer = setInterval(onCode, 5000)
    onUnmounted(() => {
      clearInterval(timer)
    })
  }
  
  
</script>